﻿#include "CGraphic.h"

CGraphic::CGraphic(void) {

}

CGraphic::CGraphic(HWND hwnd,int w,int h) {
	this->hWnd = hwnd;
	this->with = w;
	this->height = h;
	this->d3d = NULL;
	this->d3ddev = NULL;
	this->d3dxSprite = NULL;
}

bool CGraphic::InitD3D() {
	// tạo một đối tượng Direct 3D
	this->d3d = Direct3DCreate9(D3D_SDK_VERSION);
	if(!this->d3d) {
		return false;
	}
	// tạo thiết bị vẽ
	D3DPRESENT_PARAMETERS d3dpp;
	ZeroMemory(&d3dpp,sizeof(d3dpp));
	d3dpp.BackBufferCount = 1; // có một back buffer;
	d3dpp.BackBufferFormat = D3DFMT_UNKNOWN; // tự tạo format phù hợp
	d3dpp.BackBufferWidth = this->with; // chiều rộng của back buffer
	d3dpp.BackBufferHeight = this->height; // chiều cao của back buffer
	d3dpp.hDeviceWindow = this->hWnd; // handle của cửa sổ sẽ vẽ lên
	d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; // ?
	d3dpp.Windowed = true; // chế độ có cửa sổ
	HRESULT hr = this->d3d->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,this->hWnd,
											D3DCREATE_SOFTWARE_VERTEXPROCESSING,&d3dpp,&this->d3ddev);
	if(FAILED(hr)) {
		return false;
	}
	// tạo bút vẽ các texture
	hr = D3DXCreateSprite(this->d3ddev,&this->d3dxSprite);
	if(FAILED(hr)) {
		return false;
	}
	return true;
}

CGraphic::~CGraphic(void) {

}

void CGraphic::Begin() {
	// xóa cửa sổ vẽ bằng màu đen
	this->d3ddev->Clear(1,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,0),1.0f,NULL);
	// bắt đầu vẽ
	this->d3ddev->BeginScene();
	this->d3dxSprite->Begin(D3DXSPRITE_ALPHABLEND);
}

void CGraphic::End() {
	// kết thúc vẽ
	this->d3dxSprite->End();
	this->d3ddev->EndScene();
	// thể hiện các đối tượng từ back buffer ra ngoài màn hình
	this->d3ddev->Present(NULL,NULL,NULL,NULL);
}

void CGraphic::DrawTexture(LPDIRECT3DTEXTURE9 texture,D3DXVECTOR2 position,D3DCOLOR color) {
	this->d3dxSprite->Draw(texture,NULL,NULL,&D3DXVECTOR3(position.x,position.y,0),color);
}

void CGraphic::DrawTexture(LPDIRECT3DTEXTURE9 texture,D3DXVECTOR3 center,D3DXVECTOR2 position,D3DCOLOR color) {
	this->d3dxSprite->Draw(texture,NULL,&center,&D3DXVECTOR3(position.x,position.y,0),color);
}

void CGraphic::DrawTexture(LPDIRECT3DTEXTURE9 texture,RECT source,D3DXVECTOR3 center,D3DXVECTOR2 position,D3DCOLOR color) {
	this->d3dxSprite->Draw(texture,&source,&center,&D3DXVECTOR3(position.x,position.y,0),color);
}

void CGraphic::DrawTexture(LPDIRECT3DTEXTURE9 texture,D3DXVECTOR2 position,D3DCOLOR color,float angle) {
	// vừa xoay vừa vẽ, code còn bug
	/*
	D3DXMATRIX m1, m2, m3;
	this->d3dxSprite->GetTransform(&m1);
	D3DXMatrixTransformation2D(&m2,NULL,NULL,NULL,NULL,1.0f,&position);
	m3 = m1 * m2;
	this->d3dxSprite->SetTransform(&m3);
	this->d3dxSprite->Draw(texture,NULL,NULL,&D3DXVECTOR3(position.x,position.y,0),D3DCOLOR_XRGB(0,0,0));
	this->d3dxSprite->SetTransform(&m1);
	*/
}

void CGraphic::DrawSurface(LPDIRECT3DSURFACE9 surface,LPDIRECT3DSURFACE9 backbuffer,RECT rect) {
	this->d3ddev->StretchRect(surface,NULL,backbuffer,&rect,D3DTEXF_NONE);
}

LPDIRECT3DDEVICE9 CGraphic::GetDevice() {
	return this->d3ddev;
}

void CGraphic::HideCursor(bool flag) {
	this->d3ddev->ShowCursor(flag);
}
